/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;

import edu.pku.sei.modeler.editpolicies.NestedNodeLayoutEditPolicy;
import edu.pku.sei.modeler.editpolicies.UMLDiagramGraphicalNodeEditPolicy;
import edu.pku.sei.modeler.figures.ComplexStateFigure;
import edu.pku.sei.modeler.figures.SimpleStateFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.StructurePackage;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.State;
import edu.pku.sei.modeler.uml.commands.ChangeNameCommand;
import edu.pku.sei.modeler.uml.modelHelper.ModelPackage;

/**
 * @generated
 * 
 */
public class StateEditpart extends AbstractUMLNodeEditPart {
	public StateEditpart(MetaShapeElement model) {
		super(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		if (getState().getIsSimple() == true) {
			return new SimpleStateFigure();
		} else {
			return new ComplexStateFigure();
		}
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		return new DirectEditPolicy() {

			@Override
			protected Command getDirectEditCommand(DirectEditRequest arg0) {
				return new ChangeNameCommand(getState(), (String) arg0
						.getCellEditor().getValue());
			}

			@Override
			protected void showCurrentEditValue(DirectEditRequest arg0) {
				IFigure f = getDirectEditFigure();
				if (f instanceof Label) {
					((Label) f).setText(getState().getName());
				}
			}

		};
	}

	@Override
	protected IFigure getDirectEditFigure() {
		if (getState().getIsSimple() == true) {
			return ((SimpleStateFigure) this.getFigure()).getStateLable();
		} else {
			return ((ComplexStateFigure) this.getFigure()).getNameLable();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new NestedNodeLayoutEditPolicy(((MetaNode)this.getModel()).getDiagram().getType())); /*{
			@Override
			protected Command getCreateCommand(CreateRequest request) {
				MetaNode node = (MetaNode) request.getNewObject();
				MetaNode hostNode = (MetaNode) this.getHost().getModel();

				return StateMachineElementCreateCommandFactory.eINSTANCE
						.createCommand(node, hostNode,
								(Rectangle) getConstraintFor(request));
			}
		});*/
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		super.handlePropertyChanged(event);
//		if (ModelPackage.NAMESPACE_OWNEDMEMBER.equals(event.getPropertyName())
//				|| StructurePackage.DIAGRAM_NODES.equals(event
//						.getPropertyName())) {
//			this.refreshChildren();
//		}
		refreshVisuals();
	}

	private State getState() {
		return (State) ((MetaNode) getModel()).getModelElement();
	}

	protected void refreshVisuals() {
		super.refreshVisuals();
		super.refreshChildren();
		State modelEle = (State) getNode().getModelElement();
		if (modelEle.getIsSimple() == true) {
			SimpleStateFigure figure = (SimpleStateFigure) getFigure();
			figure.getStateLable().setText("" + modelEle.getName());
		} else {
			ComplexStateFigure figure = (ComplexStateFigure) getFigure();
			figure.getNameLable().setText("" + modelEle.getName());
		}

	}

	@Override
	public IFigure getContentPane() {
		if (getFigure() instanceof ComplexStateFigure) {
			return ((ComplexStateFigure) getFigure()).getBody();
		}

		return super.getFigure();
	}
}
